/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: managerOperate.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月18日 星期四 01时15分09秒
***************************************************/

#include "managerOperate.h"


//管理员操作系统
void managerOperateSystem(void){
	//会员信息变量
	VIP_M *head_vipInfo = NULL;
	FILE *fp_vipInfo = NULL;
	//会员账号密码变量
	UL *head_ul = NULL;
	FILE *fp_ul = NULL;

	int model = 0;
	logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model info]:%s",\
			__FILE__,__FUNCTION__,__LINE__,strerror(errno),"管理员操作系统");

	system("clear");

	while(1){
		printf("---管理员操作系统---\n");
		printf("1--建立会员信息（选择后原有数据清除）\n");
		printf("2--读取会员信息\n");
		printf("3--添加新会员\n");
		printf("4--查询会员\n");
		printf("5--删除会员\n");
		printf("6--修改会员信息\n");
		printf("7--会员账号挂失\n");
		printf("8--会员账号解除挂失\n");
		printf("0--退出\n");

		printf("model=");
		scanf("%d",&model);

		if(model == 0){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"0--退出");
			system("clear");
			break;
		}

		switch(model){
			case 1:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"1--建立会员信息（选择后原有数据清除）");
				createManagerToVipInfo(fp_vipInfo,head_vipInfo);
				registerUser(fp_ul,head_ul);
				break;
			case 2:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"2--读取会员信息");
				outputManagerToVipInfo(fp_vipInfo,head_vipInfo);
				break;
			case 3:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"3--添加新会员");
				fp_vipInfo = addVipInfo(fp_vipInfo,head_vipInfo);
				registerAddUser(fp_ul,head_ul);
				break;
			case 4:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"4--查询会员");
				queryManagerToVipInfo(fp_vipInfo,head_vipInfo);
				break;
			case 5:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"5--删除会员");
				fp_vipInfo = delVipInfo(fp_vipInfo,head_vipInfo);
				break;
			case 6:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"6--修改会员信息");
				fp_vipInfo = amendVipInfo(fp_vipInfo,head_vipInfo);
				break;
			case 7:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"7--会员账号挂失");
				fp_vipInfo = loseVipInfo(fp_vipInfo,head_vipInfo);
				break;
			case 8:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"8--会员账号解除挂失");
				fp_vipInfo = relieveLoseVipInfo(fp_vipInfo,head_vipInfo);
				break;
			default:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"输入错误！");
				printf("输入错误！\n");
				break;
		}
	}
}

//管理员建立会员信息函数
void createManagerToVipInfo(FILE *fp,VIP_M *head){
	head = createManagerHead();
	head = createManagerLink(head);
	outputManagerLink(head);

	fp = fopen("./vipInfo.txt","w");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	fp = vipInfoWriteToFile(fp,head);

	fclose(fp);

	freeManagerLink(head);
	printf("会员信息保存成功！\n");
}

//管理员读取会员信息函数
void outputManagerToVipInfo(FILE *fp,VIP_M *head){
	fp = fopen("./vipInfo.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	head = createManagerHead();
	head = vipInfoReadFile(fp,head);
	printf("会员信息：\n");
	outputManagerLink(head);
	fclose(fp);
	freeManagerLink(head);
	printf("会员信息读取成功！\n");
}

//会员添加函数
FILE *addVipInfo(FILE *fp,VIP_M *head){
	fp = fopen("./vipInfo.txt","a+");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerHead();
	head = createManagerLink(head);
	outputManagerLink(head);
	
	char ch[5] = {0};
	printf("请确认：yes or no?");
	scanf("%s",ch);

	if(strcmp(ch,"yes") == 0){
		fp =  vipInfoWriteToFile(fp,head);
		rewind(fp);
		freeManagerLink(head);

		head = createManagerHead();
		head = vipInfoReadFile(fp,head);
		outputManagerLink(head);
		freeManagerLink(head);
		fclose(fp);
		printf("添加成功！\n");
	}else{
		fclose(fp);
		freeManagerLink(head);
		printf("添加失败！\n");
	}
	return fp;
}

//管理员查询函数 查询成功返回id，取消返回0，未找到返回1
int queryManagerToVipInfo(FILE *fp,VIP_M *head){
	int model = 0;
	int n = 0;

	logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model info]:%s",\
			__FILE__,__FUNCTION__,__LINE__,strerror(errno),"会员信息查询系统");
	while(1){
		printf("---会员信息查询系统---\n");
		printf("1--输入账号查询\n");
		printf("2--输入姓名查询\n");
		printf("3--输入身份证号查询\n");
		printf("4--输入联系电话查询\n");
		printf("0--退出\n");

		printf("model=");
		scanf("%d",&model);

		if(model == 0){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"0--退出");
			break;
		}

		n = 0;
		switch(model){
			case 1:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"1--输入账号查询");
				printf("请输入需要查询的id\n");
				printf("id:");
				scanf("%d",&n);

				if(n >= 99999 && n <= 1000000){
					n = queryIdToVipInfo(fp,head,n);
				}else{
					printf("id输入错误！\n");
				}
				break;
			case 2:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"2--输入姓名查询");
				n = queryNameToVipInfo(fp,head);
				break;
			case 3:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"3--输入身份证号查询");
				n = queryIdNumberToVipInfo(fp,head);
				break;
			case 4:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"4--输入联系电话查询");
				n = queryTelToVipInfo(fp,head);
				break;
			default:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"输入错误！");
				printf("输入错误！\n");
				break;
		}
	}
	return n;
}

//会员删除函数
FILE *delVipInfo(FILE *fp,VIP_M *head){
	int n = 0;
	n = queryManagerToVipInfo(fp,head);
	if(n == 1){
		printf("需要删除的用户不存在！\n");
	}else{
		head = createManagerHead();
		fp = fopen("./vipInfo.txt","r");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		head = vipInfoReadFile(fp,head);
		fclose(fp);

		head = delManagerLink(head,n);
		outputManagerLink(head);

		fp = fopen("./vipInfo.txt","w");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		fp = vipInfoWriteToFile(fp,head);
		freeManagerLink(head);
		fclose(fp);

		printf("会员删除成功！\n");
	}

	return fp;
}

//会员信息修改函数
FILE *amendVipInfo(FILE *fp,VIP_M *head){
	int n = 0;
	n = queryManagerToVipInfo(fp,head);
	if(n == 1){
		printf("需要修改的用户不存在！\n");
	}else{
		head = createManagerHead();
		fp = fopen("./vipInfo.txt","r");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		head = vipInfoReadFile(fp,head);
		fclose(fp);

		head = amendManagerLink(head,n);
		outputManagerLink(head);

		fp = fopen("./vipInfo.txt","w");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		fp = vipInfoWriteToFile(fp,head);
		freeManagerLink(head);
		fclose(fp);

		printf("会员信息修改成功！\n");
	}

	return fp;
}
//会员账号挂失函数
FILE *loseVipInfo(FILE *fp,VIP_M *head){
	int n = 0;
	VIP_M *cur = NULL;
	n = queryManagerToVipInfo(fp,head);
	if(n == 1){
		printf("需要挂失的用户不存在！\n");
	}else{
		head = createManagerHead();
		fp = fopen("./vipInfo.txt","r");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		head = vipInfoReadFile(fp,head);
		fclose(fp);

		cur = head;
		while(cur != NULL){
			if(cur->data.id == n){
				if(cur->data.state != 0){
					cur->data.state = 0;
					printf("会员挂失成功！\n");
					break;
				}else{
					printf("会员账号已挂失，无需再次挂失！\n");
					break;
				}
			}
			cur = cur->next;
		}

		outputManagerLink(head);

		fp = fopen("./vipInfo.txt","w");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		fp = vipInfoWriteToFile(fp,head);
		freeManagerLink(head);
		fclose(fp);
	}
	return fp;
}
//会员账号解除挂失函数
FILE *relieveLoseVipInfo(FILE *fp,VIP_M *head){
	int n = 0;
	VIP_M *cur = NULL;
	n = queryManagerToVipInfo(fp,head);
	if(n == 1){
		printf("需要解除挂失的用户不存在！\n");
	}else{
		head = createManagerHead();
		fp = fopen("./vipInfo.txt","r");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		head = vipInfoReadFile(fp,head);
		fclose(fp);

		cur = head;
		while(cur != NULL){
			if(cur->data.id == n){
				if(cur->data.state == 0){
					cur->data.state = 1;
					printf("会员解除挂失成功！\n");
					break;
				}else{
					printf("该账号状态正常，无需解除挂失！\n");
					break;
				}
			}
			cur = cur->next;
		}

		outputManagerLink(head);

		fp = fopen("./vipInfo.txt","w");
		if(fp == NULL){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno));
			exit(-1);
		}
		fp = vipInfoWriteToFile(fp,head);
		freeManagerLink(head);
		fclose(fp);

	}
	return fp;
}
